package com.lysj.merchant.management.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.crypto.digest.BCrypt;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.RandomUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.admin.utils.web.SelectItem;
import com.lysj.merchant.desk.domain.Desk;
import com.lysj.merchant.desk.service.DeskRoomService;
import com.lysj.merchant.desk.service.DeskService;
import com.lysj.merchant.management.domain.User;
import com.lysj.merchant.management.feign.ManagementFeign;
import com.lysj.merchant.merchant.domain.Store;
import com.lysj.merchant.merchant.service.StoreService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Created by wtl on 2019-03-28 15:08
 * @description 用户业务，包括登录，找回密码
 */
@Service
public class ManagementService {

    //创建缓存，默认5秒过期
    private TimedCache<String, String> timedCache = CacheUtil.newTimedCache(5000);

    @Resource
    private UserService userService;

    @Resource
    private ManagementFeign managementFeign;

    @Resource
    private StoreService storeService;

    @Resource
    public DeskService deskService;

    @Resource
    public DeskRoomService deskRoomService;


    /**
     * @author Created by wtl on 2019/3/28 15:15
     * @Description 根据手机号发送验证码，并存到缓存中
     */
    public String getCaptcha(String phone) {
        if (ParamUtil.isBlank(phone)) {
            throw new BaseException("请输入手机号码");
        }
        User user = userService.getRepository().findByPhone(phone);
        if (ParamUtil.isBlank(user)) {
            throw new BaseException("用户不存在");
        }
        // 生成验证码
        String code = RandomUtil.getRandomNum(6);
        // 发送给用户
        boolean flag = managementFeign.sendSms(code, phone);
        if (!flag) {
            throw new BaseException("短信发送失败");
        }
        // 发送成功，验证码存到缓存
        timedCache.put(phone, code);
        return "验证码发送成功";
    }


    /**
     * @param phone   手机号码
     * @param captcha 验证码
     * @author Created by wtl on 2019/3/28 15:08
     * @Description 通过手机号修改密码
     */
    public String forgetPassword(String phone, String captcha, String password) {
        // 先从缓存中取出验证码和前端传的对比
        String code = timedCache.get(phone, false);
        System.err.println();
        if (ParamUtil.isBlank(code)) {
            throw new BaseException("验证码已过期，请重新获取");
        }
        if (!code.equals(captcha)) {
            throw new BaseException("验证码错误");
        }
        // 修改密码
        User user = userService.getRepository().findByPhone(phone);
        user.setPassword(BCrypt.hashpw(password));
        userService.save(user);
        return "成功修改密码";
    }


    public String findDeskName(String deskId) {
        Desk desk = deskService.findOne(deskId);
        return null == desk ? "" : desk.getName();
    }

    public Store findStoreInfo(String storeId) {
        Store store = storeService.findOne(storeId);
        return store;
    }

}
